package string;

/**
 * @author: june
 * @date: 2023/8/10
 * @description: 1768. 交替合并字符串
 * 给你两个字符串 word1 和 word2 。请你从 word1 开始，通过交替添加字母来合并字符串。
 * 如果一个字符串比另一个字符串长，就将多出来的字母追加到合并后字符串的末尾。
 * <p>
 * 返回 合并后的字符串 。
 **/

public class MergeStringAlternately_1768 {
    public static void main(String[] args) {
        MergeStringAlternately_1768 mergeStringAlternately_1768 = new MergeStringAlternately_1768();
        String word1 = "abcd", word2 = "pq";
        String s = mergeStringAlternately_1768.mergeAlternately(word1, word2);
        System.out.println(s);
    }

    public String mergeAlternately(String word1, String word2) {
        StringBuilder sb = new StringBuilder();

        int i = 0;
        for (; i < word1.length(); i++) {
            sb.append(word1.charAt(i));
            if (word2.length() > i) {
                sb.append(word2.charAt(i));
            }
        }

        if (word2.length() > i) {
            sb.append(word2.substring(i));
        }
        return sb.toString();
    }
}
